﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;

namespace PLCS.App.Shared.Common;

public class AnimationHelper
{
    /// <summary>
    /// 控件绕中心旋转动画
    /// </summary>
    /// <param name="element">控件名</param>
    /// <param name="anglefrom">开始角度</param>
    /// <param name="angleto">结束角度</param>
    /// <param name="power">过渡强度1为不用过渡</param>
    /// <param name="time">持续时间，例如3秒： TimeSpan(0,0,3) </param>
    public static void AngleRotateAnimation(UIElement element, double anglefrom, double angleto, int power, TimeSpan time)
    {
        RotateTransform angle = new RotateTransform();  //旋转
        element.RenderTransform = angle;
        //定义圆心位置
        element.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
        //定义过渡动画,power为过度的强度
        EasingFunctionBase easeFunction = new PowerEase()
        {
            EasingMode = EasingMode.EaseInOut,
            Power = power
        };

        DoubleAnimation angleAnimation = new DoubleAnimation()
        {
            From = anglefrom,                                 //起始值
            To = angleto,                                     //结束值
            FillBehavior = FillBehavior.HoldEnd,
            Duration = time,                                  //动画播放时间
            //RepeatBehavior = RepeatBehavior.Forever,        //一直播放
        };
        angle.BeginAnimation(RotateTransform.AngleProperty, angleAnimation);
    }

    /// <summary>
    /// 控件绕中心旋转动画
    /// </summary>
    /// <param name="element">控件</param>
    /// <param name="direction">移动方向</param>
    /// <param name="from">开始</param>
    /// <param name="to">结束</param>
    /// <param name="power">过渡强度1为不用过渡</param>
    /// <param name="time">持续时间，例如3秒： TimeSpan(0,0,3) </param>
    public static void TranslationAnimation(UIElement element, string direction, double from, double to, int power, TimeSpan time)
    {
        TranslateTransform translate = new TranslateTransform();
        element.RenderTransform = translate;
        //定义过渡动画,power为过度的强度
        EasingFunctionBase easeFunction = new PowerEase()
        {
            EasingMode = EasingMode.EaseInOut,
            Power = power
        };

        DoubleAnimation angleAnimation = new DoubleAnimation()
        {
            From = from,                                 //起始值
            To = to,                                     //结束值
            FillBehavior = FillBehavior.HoldEnd,
            Duration = time,                                  //动画播放时间
            //RepeatBehavior = RepeatBehavior.Forever,        //一直播放
        };
        if ("x".Equals(direction))
            translate.BeginAnimation(TranslateTransform.XProperty, angleAnimation);
        else
            translate.BeginAnimation(TranslateTransform.YProperty, angleAnimation);
    }

    /// <summary>
    /// RGV行驶动画（从当前目标出发）
    /// </summary>
    /// <param name="element"></param>
    /// <param name="direction"></param>
    /// <param name="to"></param>
    /// <param name="power"></param>
    /// <param name="time"></param>
    public static void TranslationAnimation(UIElement element, string direction, double to, int power, TimeSpan time)
    {
        TranslateTransform translate = new TranslateTransform();
        element.RenderTransform = translate;
        //定义过渡动画,power为过度的强度
        EasingFunctionBase easeFunction = new PowerEase()
        {
            EasingMode = EasingMode.EaseInOut,
            Power = power
        };

        DoubleAnimation angleAnimation = new DoubleAnimation()
        {
            To = to,                                     //结束值
            //FillBehavior = FillBehavior.HoldEnd,
            Duration = time,                                  //动画播放时间
            //RepeatBehavior = RepeatBehavior.Forever,        //一直播放
        };
        if ("x".Equals(direction))
            translate.BeginAnimation(TranslateTransform.XProperty, angleAnimation);
        else
            translate.BeginAnimation(TranslateTransform.YProperty, angleAnimation);
    }

    /// <summary>
    /// 移动动画
    /// </summary>
    /// <param name="top">目标点相对于上端的位置</param>
    /// <param name="left">目标点相对于左端的位置</param>
    /// <param name="elem">移动元素</param>
    public static void FloatInElement(double value, Direction direction, UIElement elem, TimeSpan time)
    {
        try
        {
            if (direction == Direction.X)
            {
                DoubleAnimation floatX = new DoubleAnimation()
                {
                    To = value,
                    Duration = time,
                };
                elem.BeginAnimation(Canvas.LeftProperty, floatX);
            }
            else
            {
                DoubleAnimation floatY = new DoubleAnimation()
                {
                    To = value,
                    Duration = time,
                };

                elem.BeginAnimation(Canvas.TopProperty, floatY);
            }
        }
        catch (Exception ex)
        {
        }
    }
}

public enum Direction
{
    X, Y
}